package com.quectel.core.module.patrol.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.core.module.patrol.dao.VideoPatrolPlanDao;
import com.quectel.core.module.patrol.dto.VideoPatrolPlanDto;
import com.quectel.core.module.patrol.dto.VideoPatrolRouteDto;
import com.quectel.core.module.patrol.entity.VideoPatrolPlanEntity;
import com.quectel.core.module.patrol.service.VideoPatrolPlanService;
import com.quectel.core.module.patrol.service.VideoPatrolRouteService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2020-07-07 15:47:28
 */
@DubboService
public class VideoPatrolPlanServiceImpl implements VideoPatrolPlanService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VideoPatrolPlanServiceImpl.class);

    @Autowired
    private VideoPatrolPlanDao videoPatrolPlanDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private VideoPatrolRouteService videoPatrolRouteService;


    @Override
    public List<VideoPatrolPlanDto> selectByRouteId(Long routeId) {
        List<VideoPatrolPlanEntity> entityList = videoPatrolPlanDao.selectList(
                new QueryWrapper<VideoPatrolPlanEntity>().eq("video_patrol_route_id",
                        routeId));
        return CopyUtils.copyList(entityList, VideoPatrolPlanDto.class);
    }

    @Override
    public List<VideoPatrolPlanDto> queryList(Map<String, Object> params) {
        List<VideoPatrolPlanEntity> list = videoPatrolPlanDao.queryList(params);
        List<VideoPatrolPlanDto> result = CopyUtils.copyList(list, VideoPatrolPlanDto.class);
        for (VideoPatrolPlanDto videoPatrolPlanDto : result) {
            VideoPatrolRouteDto videoPatrolRouteDto = videoPatrolRouteService.selectById(Long.parseLong(videoPatrolPlanDto.getVideoPatrolRouteId()));
            if (videoPatrolPlanDto != null) {
                videoPatrolPlanDto.setVideoPatrolRouteDto(videoPatrolRouteDto);
            }
            VillageDto villageDto = villageService.selectById(videoPatrolPlanDto.getVillageId());
            if (villageDto != null) {
                videoPatrolPlanDto.setVillageDto(villageDto);
            }

        }
        return result;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return videoPatrolPlanDao.queryTotal(params);
    }

    @Override
    public Long save(VideoPatrolPlanDto dto) {
        VideoPatrolPlanEntity entity = CopyUtils.copyObj(dto, VideoPatrolPlanEntity.class);
        entity.setId(Snowflake.nextId());
        videoPatrolPlanDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(VideoPatrolPlanDto dto) {
        VideoPatrolPlanEntity entity = CopyUtils.copyObj(dto, VideoPatrolPlanEntity.class);
        videoPatrolPlanDao.updateById(entity);
    }

    @Override
    public VideoPatrolPlanDto selectById(Long id) {
        VideoPatrolPlanEntity entity = videoPatrolPlanDao.selectById(id);
        return CopyUtils.copyObj(entity, VideoPatrolPlanDto.class);
    }

    @Override
    public void deleteById(Long id) {
        videoPatrolPlanDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
